<?php

namespace DebugComponent;

/**
 * Parameter holder class
 * Helper class for passing data
 */
class ParameterHolder {

    /**
     * Class constructor initialize parameters array
     */
    public function __construct() {
        $this->parameters = array();
    }

    /**
     * Assign value to key in selected path
     *
     * @param string $key
     * @param mixed $value
     * @param string $path
     * @return void
     */
    public function set($key, $value, $path='/') {
        if (!isset($this->parameters[$path])) {
            $this->parameters[$path] = array();
        }
        $this->parameters[$path][$key] = $value;
    }

    /**
     * Rretrieve value assigned to specified key
     * If value not found then default value is returned
     *
     * @param string $key
     * @param string $path
     * @param mixed $defaultValue
     * @return mixed
     */
    public function get($key, $path='/', $defaultValue=null) {
        return isset($this->parameters[$path]) && isset($this->parameters[$path][$key])
                ? $this->parameters[$path][$key] : $defaultValue;
    }

    /**
     * Determine if specified key is defined
     *
     * @param string $key
     * @param string $path
     * @return boolean
     */
    public function has($key, $path='/') {
        return isset($this->parameters[$path]) && isset($this->parameters[$path][$key])
                ? true : false;
    }

    /**
     * Retrieve all keys defined in specified path
     *
     * @param string $path
     * @param mixed $defaultValue
     * @return array|mixed
     */
    public function getPath($path='/', $defaultValue=array()) {
        return isset($this->parameters[$path]) ? $this->parameters[$path] : $defaultValue;
    }

    /**
     * Determine if specified path is defined
     *
     * @param string $path
     * @return boolean
     */
    public function hasPath($path='/') {
        return isset($this->parameters[$path]);
    }

    /**
     * @var array parameters array
     */
    private $parameters;
}